package com.steven.team.controller;

import com.alibaba.fastjson.JSONObject;
import com.steven.team.service.ChatService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@RestController
public class SendMessageController {
    @Autowired
    ChatService chatService;

    @PostMapping(value = "/chat/message/send")
    public JSONObject send(@RequestHeader Long uid, Long receiveUser, String message, HttpServletResponse response) throws IOException {
        if (uid == null || uid == 0 || receiveUser == null || receiveUser == 0 || message == null || message.isEmpty()) {
            response.sendError(500, "传参错误");
            return null;
        } else {
            response.setHeader("Content-Type", "application/json");
            return chatService.addMessage(uid, receiveUser, message);
        }
    }

    @PostMapping(value = "/chat/message/queryAll")
    public JSONObject queryAll(@RequestHeader long uid, long friendId, long lastCreateTime, HttpServletResponse response) throws IOException {
        if (uid == 0 || friendId == 0) {
            response.sendError(500, "传参错误");
            return null;
        } else {
            response.setHeader("Content-Type", "application/json");
            return chatService.queryAll(uid, friendId, lastCreateTime);
        }
    }

    @PostMapping(value = "/chat/message/queryAllUnread")
    public JSONObject queryAllUnread(@RequestHeader long uid, HttpServletResponse response) throws IOException {
        if (uid == 0) {
            response.sendError(500, "传参错误");
            return null;
        } else {
            response.setHeader("Content-Type", "application/json");
            return chatService.queryAllUnread(uid);
        }
    }

    @PostMapping(value = "/chat/message/updateStatus")
    public JSONObject updateMessageStatusBy2Users(@RequestHeader Long uid, Long sendUser, HttpServletResponse response) throws IOException {
        if (uid == null || uid == 0 || sendUser == null || sendUser == 0) {
            response.sendError(500, "传参错误");
            return null;
        } else {
            response.setHeader("Content-Type", "application/json");
            return chatService.updateMessageStatusBy2Users(uid, sendUser);
        }
    }
}
